/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.praise;

import java.io.File;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.boxin.framework.base.utils.Constants;
import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.momarketing.common.QRCodeUtils;
import com.boxin.ims.modules.momarketing.entity.QRCode;
import com.boxin.ims.modules.momarketing.service.QRCodeService;
import com.boxin.ims.modules.wechat.entity.praise.PraiseActivity;
import com.boxin.ims.modules.wechat.dao.praise.PraiseActivityDao;

/**
 * 赞活动Service
 * @author Jakemanse
 * @version 2014-01-03
 */
@Component
@Transactional(readOnly = true)
public class PraiseActivityService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(PraiseActivityService.class);
	
	@Autowired
	private PraiseActivityDao praiseActivityDao;
	
	@Autowired
	private QRCodeService qrCodeService;
	
	public PraiseActivity get(Long id) {
		return praiseActivityDao.findOne(id);
	}
	
	public Page<PraiseActivity> find(Page<PraiseActivity> page, PraiseActivity praiseActivity) {
		DetachedCriteria dc = praiseActivityDao.createDetachedCriteria();
		if (praiseActivity.getUser()!=null && praiseActivity.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", praiseActivity.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(praiseActivity.getName())){
			dc.add(Restrictions.like("name", "%"+praiseActivity.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return praiseActivityDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(PraiseActivity praiseActivity) {
		
		int width = QRCodeUtils.WIDTH;
		int height =QRCodeUtils.HEIGHT;
//		UserDefine userDefine  = userDefineService.getByUserId(circleShare.getUser().getId());
//		if(userDefine!=null){
//			if(userDefine.getQrcodeHeight()!=null){
//				height = userDefine.getQrcodeHeight();
//			}
//			if(userDefine.getQrcodeWidth()!=null){
//				width = userDefine.getQrcodeWidth();
//			}
//		}
		
		//生成二维码
		QRCode qrcode = new QRCode();
		String content = QRCodeUtils.generatePraiseActivitySN(praiseActivity.getUser().getId());
		qrcode.setContent(content);
		String path = Constants.getWechatResourceSavePath();
		path += DateUtils.getTodayString()+File.separator+content;
		FileUtils.createDirectory(path);
		QRCodeUtils.generateQRCodePNG(content, path, width, height);
		qrcode.setFileName(content+".png");
		qrcode.setPngPath(path+".png");
		qrCodeService.save(qrcode);	//保存二维码
		
		praiseActivity.setQrCode(qrcode);
		praiseActivityDao.save(praiseActivity);
		
		
		praiseActivityDao.save(praiseActivity);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		praiseActivityDao.deleteById(id);
	}
	
}
